package edu.boun.g4.coursity.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.CoursityUser;
import edu.boun.g4.coursity.domain.Rating;

import edu.boun.g4.coursity.dto.CoursityUserDto;
import edu.boun.g4.coursity.service.CourseService;
import edu.boun.g4.coursity.service.CommentService;
import edu.boun.g4.coursity.service.RatingService;
import edu.boun.g4.coursity.service.UserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author mertusta
 * @author sebnemarslan
 *         Handles and retrieves user request
 */
@Controller
@RequestMapping("/coursityUser")
public class CoursityUserController {

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "courseService")
	private CourseService courseService;

	@Resource(name = "commentService")
	private CommentService commentService;
	
	@Resource(name = "ratingService")
	private RatingService ratingService;
	
	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public @ResponseBody
	Response addUser( HttpServletResponse httpResponse,
			@RequestParam String email, @RequestParam String password
			) {

		CoursityUser coursityUser = new CoursityUser();
		coursityUser.setName(email);
		coursityUser.setUsername(email);
		coursityUser.setEmail(email);
		coursityUser.setPassword(password);
		coursityUser.setActive(true);
		// coursityUser.setComments(null);
		// coursityUser.setFavouriteCourses(null);
		// coursityUser.setFollowing(null);
		// coursityUser.setRatings(null);

		Response response = new Response();
	//	response.setBody(coursityUser);
		response.setStatus(true);

		userService.add(coursityUser);
		
		Cookie c1 = new Cookie("email", email);
		c1.setMaxAge(65000);
		httpResponse.addCookie(c1);
		Cookie c2 = new Cookie("id", Integer.toString(coursityUser.getId()));
		c2.setMaxAge(65000);
		httpResponse.addCookie(c2);
	
		
		return response;
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/login")
	public @ResponseBody
	Response login( HttpServletResponse httpResponse,
			@RequestParam String email, @RequestParam String password
			) {

		CoursityUser coursityUser = userService.getByMail(email);
		Response response = new Response();
		response.setStatus(false);
		//	response.setBody(coursityUser);
		try{
			if(coursityUser.getPassword().equals(password)){
				
				Cookie c1 = new Cookie("email", email);
				c1.setMaxAge(65000);
				httpResponse.addCookie(c1);
				Cookie c2 = new Cookie("id", Integer.toString(coursityUser.getId()));
				c2.setMaxAge(65000);
				httpResponse.addCookie(c2);
				response.setStatus(true);
				response.setBody(coursityUser.getId());
				
			}else
				response.setErrorStr("User or password error!");
		}catch(Exception e){
			
			
			response.setErrorStr("User or password error!");
			
		}		
		
		return response;
	}


	@RequestMapping(method = RequestMethod.POST, value = "/delete")
	public @ResponseBody
	Response deleteUser(@RequestParam Integer id) {

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		userService.delete(id);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public @ResponseBody
	Response editUser(@RequestParam String name, @RequestParam String username,
			@RequestParam String email, @RequestParam Boolean active) {

		CoursityUser existingUser = new CoursityUser();
		existingUser.setName(name);
		existingUser.setUsername(username);
		existingUser.setEmail(email);
		// existingUser.setPassword(password);
		existingUser.setActive(active);
		// existingUser.setComments(null);
		// existingUser.setFavouriteCourses(null);
		// existingUser.setFollowing(null);
		// existingUser.setRatings(null);

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		userService.edit(existingUser);

		return response;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Response listUsers() {

		List<CoursityUser> all = userService.getAll();
		List<CoursityUserDto> dtos = new ArrayList<CoursityUserDto>();

		CoursityUserDto dto;
		for (CoursityUser coursityUser : all) {
			dto = new CoursityUserDto();
			dto.setActive(coursityUser.isActive());
			dto.setComments(commentService.getAllByUser(coursityUser.getId()));
			dto.setEmail(coursityUser.getEmail());
			//dto.setFavouriteCourses(courseService.getAllFavourite(coursityUser.getId()));
			//dto.setFollowing(userService.getAllFollowing(coursityUser.getId()));
			dto.setId(coursityUser.getId());
			dto.setName(coursityUser.getName());
			// dto.setPassword(password)
			//dto.setRatings(ratingService.getAllByUser(coursityUser.getId()));
			dto.setUsername(coursityUser.getUsername());
			dtos.add(dto);
		}

		Response response = new Response();
		response.setBody(dtos);
		response.setStatus(true);

		return response;
	}

	@RequestMapping(value = "/createTest")
	public @ResponseBody
	Response test() {

		CoursityUser existingUser = new CoursityUser();
		existingUser.setName("mert");
		existingUser.setUsername("mert");
		existingUser.setEmail("ustamert@yahoo.com");
		existingUser.setPassword("123456");
		existingUser.setActive(true);
		userService.add(existingUser);

		CoursityUser existingUser2 = new CoursityUser();
		existingUser2.setName("bulut");
		existingUser2.setUsername("cloud");
		existingUser2.setEmail("bulut@yahoo.com");
		existingUser2.setPassword("123456");
		existingUser2.setActive(true);
		userService.add(existingUser2);
		
		CoursityUser existingUser3 = new CoursityUser();
		existingUser3.setName("sebnem");
		existingUser3.setUsername("sebnema");
		existingUser3.setEmail("sebnem@sebnem.com");
		existingUser3.setPassword("123456");
		existingUser3.setActive(true);
		userService.add(existingUser3);
		
		CoursityUser existingUser4 = new CoursityUser();
		existingUser4.setName("jolie");
		existingUser4.setUsername("jolie");
		existingUser4.setEmail("jolie@jolie.com");
		existingUser4.setPassword("123456");
		existingUser4.setActive(true);
		userService.add(existingUser4);
		
		CoursityUser existingUser5 = new CoursityUser();
		existingUser5.setName("bart");
		existingUser5.setUsername("bart");
		existingUser5.setEmail("bart@bart.com");
		existingUser5.setPassword("123456");
		existingUser5.setActive(true);
		userService.add(existingUser5);

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		return response;
	}
	
	@RequestMapping(value = "/testGetUserByRating")
	public @ResponseBody
	Response testGetByRating() {

		Integer userId = userService.getByRating(1);
		
		Response response = new Response();
		response.setBody(userId);
		response.setStatus(true);

		return response;
	}

	
}